<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * Main Controller
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @since September, 2011
 */
class Link extends CI_Controller{


	/**
	 * @desc Array - datas which should pass to the views
	 */
	public $data = array();



	/**
	 * @desc Controller Constructor
	 * This will check the session to see if the user is logged in or what
	 * And will load the related language files
	 */
	public function __construct(){
		parent::__construct();
	}




	/**
	 * @desc This will get called when no method specified
	 * Will show a welcome page
	 */
	public function index(){

		$this->load->model("link_model","linkModel");

		$items = $this->linkModel->getRecords(array("enable"=>"1"), 0, 3);
		$this->data["items"] = array();

		foreach($items as $item){
			if(isset($item->image) && ! empty($item->image)){

				if( ! isset($SI)){
					$this->load->helper("simple_image");
					$SI = new SimpleImage();
				}

				$item->image = $SI->getThumbPathWidthHeight($item->image,0,100);
			}

			$this->data["items"][] = $item;
		}

		$this->data["content"] = $this->load->view("view_link",$this->data,TRUE);
		$this->data["menuItem"] = "linkMenuItem"; //we use this value ,which send to container view,in order to know what the page is! 
		$this->load->view("container",$this->data);
	}



	public function view($id){
		$this->load->model("link_model","linkModel");

		$item = $this->linkModel->getRecord("id",$id);

		if(isset($item->image) && ! empty($item->image)){

			$this->load->helper("simple_image");
			$SI = new SimpleImage();

			$item->image = $SI->getThumbPathWidthHeight($item->image,200,0);
		}

		$this->data["item"] = $item;
		
		//
		$this->load->model("comment_model","commentModel");
		$this->data["comment"]="salam";
		//sizeof($this->commentModel->getRecords(array("table_name"=>"home","row_id"=>$id)));
		//
		
		$this->data["content"] = $this->load->view("view_link",$this->data,TRUE);
		$this->load->view("container",$this->data);
	}

	/*
	public function getComments($itemId){
		$this->load->model("comment_model","commentModel");
		return $this->commentModel->getRecords(array("table_name"=>"home","row_id"=>$itemId));
	}
	*/

	public function comment($itemId){

		$this->load->model("comment_model","commentModel");
		$this->data["comments"]=$this->commentModel->getRecords(array("table_name"=>"link","row_id"=>$itemId));
		
		//$this->data["comments"]=getComments($itemId());
		
		$this->load->model("link_model","linkModel");
		$this->load->helper("form");

		$this->data["item"] = $this->linkModel->getRecord("id",$itemId);

		$this->data["content"] = $this->load->view("form_comment",$this->data,TRUE);
		$this->load->view("container",$this->data);
	}



	public function captcha(){
		$this->load->helper("phpcaptcha");
		$PC = new PhpCaptcha(array("MAIAN.TTF"),$this->session);
		$PC->Create();
	}

	public function sendMail($param) {
		$this->load->library('email');

		$this->email->from('your@example.com', 'Your Name');
		$this->email->to('someone@example.com'); 
		$this->email->cc('another@another-example.com'); 
		$this->email->bcc('them@their-example.com'); 
		
		$this->email->subject('Email Test');
		$this->email->message('Testing the email class.');	
		
		$this->email->send();
		
		echo $this->email->print_debugger();
	}
	

	public function submitComment(){
		
		$name = trim($this->input->post("name"));
		$email = trim($this->input->post("email"));
		$comment = trim($this->input->post("comment"));
		$itemId = trim($this->input->post("itemId"));
		$captcha = trim($this->input->post("captcha"));
		
		if(empty($captcha) || empty($name) || empty($email) || empty($comment) || empty($itemId) ){

			$this->data["formErrors"][] = "Required fields are empty.";
		}else{
			//TODO delete these two useless echo
			//echo $this->session->userdata("php_captcha")."<br>";
			//echo strtolower($captcha);
			if(strtolower($captcha) == strtolower($this->session->userdata("php_captcha"))){

				
				
				$userFieldsValues = array(
					"email"=>$email,
					"user_name"=>$name,
				);
				
				$this->load->model("user_model","userModel");
				$result = $this->userModel->insert($userFieldsValues);
				if($result){
					$user=$this->userModel->getRecord("email",$email);
					
					$fieldsValues = array(
						"table_name"=>"link",
						"row_id"=>$itemId,
						"comment"=>$comment,
						"FK_user_id"=>$user->id
					);
		
					$this->load->model("comment_model","commentModel");
		
					$result = $this->commentModel->insert($fieldsValues);
		
					if($result){
						$this->data["formMessages"][] = "Ok. Comment saved.";
					}else{
						$this->data["formErrors"][] = "Faild.";
					}
				}
			}else{
				$this->data["formErrors"][] = "Captcha wrong.";
			}
		}

		$this->comment($itemId);
	}

}